/**
  ******************************************************************************
  * @file    proj_main.c 
  * @author  SD Electronics 
  * @version V1.0
  * @date    2022-04-19
  * @brief   每次更新SDK需要将此文件移植.
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "gap_api.h"
#include "gatt_api.h"
#include "os_timer.h"
#include "os_mem.h"
#include "sys_utils.h"
#include "button.h"
#include "jump_table.h"
#include "user_task.h"
#include "driver_plf.h"
#include "driver_system.h"
#include "driver_i2s.h"
#include "driver_iic.h"
#include "driver_pmu.h"
#include "driver_uart.h"
#include "driver_rtc.h"
#include "driver_flash.h"
#include "driver_efuse.h"
#include "flash_usage_config.h"
#include "ble_simple_peripheral.h"
#include "simple_gatt_service.h"
#include "sd_app_dev.h"
#include "sd_app_007DJBC.h"
#include "driver_adc.h"
#include "SH3001.h"
#include "user_task.h"
#include "sd_common.h"

/** @defStruct\Enum 配置固件版本号
  * @{
  */ 
const struct jump_table_version_t _jump_table_version __attribute__((section("jump_table_3"))) = 
{
    .firmware_version = 0x00000000,
};

/** @defStruct\Enum 配置OTA用升级bin文件大小，根据实际bin文件计算
  * @{
  */ 
const struct jump_table_image_t _jump_table_image __attribute__((section("jump_table_1"))) =
{
    .image_type = IMAGE_TYPE_APP,
    .image_size = 0x20000,      
};

/**
  * @}
  */ 

/** @defStruct\Enum initial_static_memory
  * @{
  */ 
void initial_static_memory(uint8_t act_num, 
                                    uint8_t adv_act_num, 
                                    uint8_t con_num, 
                                    uint8_t rx_buf_num, 
                                    uint16_t rx_buf_size, 
                                    uint8_t tx_buf_num, 
                                    uint16_t tx_buf_size,
                                    uint16_t max_adv_size,
                                    uint16_t stack_size);

/**
  * @brief  user_init_static_memory
  * @note   设置多连接
  * @param  None
  * @retval None
  */
void user_init_static_memory(void)
{
    /*
     * activity: init, adv+con, adv1, scan, pb-adv, pb-gatt (adv + con)
     * adv: adv * 2 * 2, adv1 * 2, pb-adv + 1, pb-gatt *2 * 2
     * con: init, pb-gatt*2
     */
    initial_static_memory(8, 11, 3, 24, 40, 8, 27, 37, 0x1000);
}


#if 0/* 外部中断函数 */
/**
  * @brief  pmu_gpio_isr_ram
  * @note   唤醒用的gpio中断处理函数-上升下降沿都可以触发
  * @note   可以配置按键 检测高低电平(需要用户处理)
  * @param  None
  * @retval None
  */
__attribute__((section("ram_code"))) void pmu_gpio_isr_ram(void)
{
    uint32_t gpio_value = ool_read32(PMU_REG_GPIOA_V);
    button_toggle_detected(gpio_value);
    ool_write32(PMU_REG_PORTA_LAST, gpio_value);
}
#endif

/**
  * @brief  user_custom_parameters
  * @note   用户定义参数：随机的MAC地址 主频
  * @param  None
  * @retval None
  */
void user_custom_parameters(void)
{
    struct chip_unique_id_t id_data;
    efuse_get_chip_unique_id(&id_data);
    id_data.unique_id[5] |= 0xc0; // random addr->static addr type:the top two bit must be 1.
    memcpy(__jump_table.addr.addr,id_data.unique_id, 6);
    __jump_table.system_clk = SYSTEM_SYS_CLK_48M;
    jump_table_set_static_keys_store_offset(JUMP_TABLE_STATIC_KEY_OFFSET);
    retry_handshake();
}


/**
  * @brief  user_entry_before_sleep_imp
  * @note   进入休眠之前首先进入的函数
  * @param  None
  * @retval None
  */
__attribute__((section("ram_code"))) void user_entry_before_sleep_imp(void)
{
	uart_putc_noint_no_wait(UART1, 's');
}

/**
  * @brief  user_entry_after_sleep_imp
  * @note  休眠唤醒首先进入的函数
  * @param  None
  * @retval None
  */
__attribute__((section("ram_code"))) void user_entry_after_sleep_imp(void)
{
    system_set_port_mux(GPIO_PORT_A, GPIO_BIT_3, PORTA3_FUNC_UART1_TXD);
    uart_init(UART1, BAUD_RATE_115200);
    uart_putc_noint_no_wait(UART1, 'w');
    NVIC_EnableIRQ(UART0_IRQn);
    NVIC_EnableIRQ(PMU_IRQn);
}

/**
  * @brief  user_entry_before_ble_init
  * @note  协议栈初始化之前的：可以配置LDO ANT功率 充电电压电流
  * @param  None
  * @retval None
  */
void user_entry_before_ble_init(void)
{    
    //set system power supply in BUCK mode
    pmu_set_sys_power_mode(PMU_SYS_POW_BUCK);
    pmu_set_aldo_voltage(PMU_ALDO_MODE_NORMAL, PMU_ALDO_VOL_3_5);
    system_set_tx_power(RF_TX_POWER_0dBm);
#ifdef FLASH_PROTECT
    flash_protect_enable(1);
#endif
    pmu_enable_irq(PMU_ISR_BIT_ACOK
                   | PMU_ISR_BIT_ACOFF
                   | PMU_ISR_BIT_ONKEY_PO
                   | PMU_ISR_BIT_OTP
                   | PMU_ISR_BIT_LVD
                   | PMU_ISR_BIT_BAT
                   | PMU_ISR_BIT_ONKEY_HIGH);
    NVIC_EnableIRQ(PMU_IRQn);
    
    // Enable UART print.
    system_set_port_mux(GPIO_PORT_A, GPIO_BIT_3, PORTA3_FUNC_UART1_TXD);
    uart_init(UART1, BAUD_RATE_115200);

    // user code...
}

/**
  * @brief  user_entry_after_ble_init
  * @note  用户主程序代码
  * @param  None
  * @retval None
  */
void user_entry_after_ble_init(void)
{
    system_sleep_disable();
    co_printf("\r\nSDCodeTemplate.\r\n");
    SDDevInit(0);
    SDAppATInit(); 
    //SD_APP_032FBL_TaskInit(&st_FBL_Ctrl);
}

/******************* (C) COPYRIGHT 2011 ShenZhenShiding electronics **************************/
